#!coding=utf-8
import time
import threading
import requests
from concurrent.futures import ThreadPoolExecutor,wait,ALL_COMPLETED
import asyncio
import aiohttp

webps_url = [
    'https://pic3.zhimg.com/80/v2-f35dbed8a1a5eb4e00eef18068a53686_720w.webp',
    'https://pic1.zhimg.com/80/v2-f0a5cbb7ee4e3756891df9498a3e4100_720w.webp',
    'https://pic4.zhimg.com/80/v2-fef3ea9747a38dc3581d74e824f72677_720w.webp',
    'https://pic3.zhimg.com/80/v2-c34380585d65e2d51858b800a294cf3a_720w.webp',
    'https://pic3.zhimg.com/80/v2-52d31682b8428efb2cdc4788f32c9e4a_720w.webp',
    'https://pic3.zhimg.com/80/v2-acf52c0da12685be01712ccceb6b3f12_720w.webp',
    'https://pic2.zhimg.com/80/v2-effff3856f5dd93396052273bae8f721_720w.webp',
    'https://pic3.zhimg.com/80/v2-9c2dd6d4dbb968b802dd69de4c0f9b1e_720w.webp',
    'https://pic2.zhimg.com/80/v2-d62443d5f73ae152a07596bca87f4175_720w.webp',
    'https://pic4.zhimg.com/80/v2-161c2a6ab7aaa6408c04007ab98e35a7_720w.webp'
]


def normal_download():
    prefix = 'normal--'
    begin_time = time.time()
    for url in webps_url:
        response = requests.get(url)
        with open(prefix + url.split('/')[-1], 'wb') as f:
            f.write(response.content)
    end_time = time.time()
    print('普通下载时间: %.2f' % (end_time - begin_time))

def thread_download_function( download_url ):
    prefix = 'thread--'
    response = requests.get(download_url)
    with open(prefix + download_url.split('/')[-1], 'wb') as f:
        f.write(response.content)

def thread_poll_download_function( download_url ):
    prefix = 'pool--'
    response = requests.get(download_url)
    with open(prefix + download_url.split('/')[-1], 'wb') as f:
        f.write(response.content)

def thraed_download():
    threads = []
    begin_time = time.time()
    for item in webps_url:
        t = threading.Thread(target=thread_download_function, args=(item,))
        threads.append(t)

    for th in threads:
        th.start()

    for th in threads:
        th.join()

    end_time = time.time()
    print('多线程下载时间: %.2f' % (end_time - begin_time))

def thread_pool_download():
    tasks = []
    begin_time = time.time()
    with ThreadPoolExecutor(max_workers=10) as pool:
        for item in webps_url:
            tasks.append(pool.submit(thread_poll_download_function, item))

        # 主线程等待所有子线程完成
        wait(tasks, return_when=ALL_COMPLETED)
    end_time = time.time()
    print('线程池下载时间: %.2f' % (end_time - begin_time))


async def async_download_file(session, download_url):
    async with session.get(download_url) as response:
        content = await response.content.read()
        prefix = 'aio--'
        with open(prefix + download_url.split('/')[-1], 'wb') as f:
            f.write(content)
async def async_download():
    begin_time = time.time()
    async with aiohttp.ClientSession() as session:
        tasks = [asyncio.create_task(async_download_file(session, url)) for url in webps_url]
        await asyncio.gather(*tasks)
    end_time = time.time()
    print('协成下载时间: %.2f' % (end_time - begin_time))

if __name__ == '__main__':
    normal_download()
    thraed_download()
    thread_pool_download()
    asyncio.run(async_download())


